package com.weixin.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登陆过滤器
 */
@WebFilter("/*")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        String url = request.getRequestURL().toString();
        HttpSession session = request.getSession();
        if(url.contains("jsp")&&url.contains("login.jsp")||url.contains("Servlet")&&url.contains("LoginServlet/*")||url.endsWith("/")){
            if(session.getAttribute("admin")!=null){
                chain.doFilter(request,response);
            }else{
               // request.setAttribute("err","输入用户名或者密码不对!");
                request.getRequestDispatcher("/pages/houtai/login.jsp").forward(request,response);
            }
        }else{
            chain.doFilter(request,response);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
